package com.wms.controller;

import com.wms.annotation.MyLog;
import com.wms.entity.SysUser;
import com.wms.service.SysUserService;
import com.wms.util.PwdEncrypt;
import com.wms.util.R;
import com.wms.util.RsaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.UUID;

/**
 * ---Do First---
 *
 * @author:wellseasun
 * @date:2025/4/25 下午 2:04
 * @desc:
 */
@RestController
@RequestMapping("/sys")
public class SysUserController {


    @Autowired
    SysUserService sysUserService;

    //暂时存放token的键值对，以后要用redis来存
    private HashMap<String, Object> tokenMap = new HashMap<>() ;

    /**
     * 登录
     * @return
     */
    @MyLog(value = "登录", type = "查询")
    @PostMapping("/user/login")
    public R login(@RequestBody SysUser sysUser, HttpSession session) throws Exception {
        //先解密
        String pwd = RsaUtils.decryptByPrivateKey(sysUser.getPassword( ));
        sysUser.setPassword(pwd);
        // 再对前端传过来的password加密
        String pwd2 = PwdEncrypt.hashPassword(sysUser.getPassword( ));
        sysUser.setPassword(pwd2);
        // 根据用户名+密码查数据库,将信息存储到redis,查到返回token
        SysUser loginUser = sysUserService.login(sysUser);
        if (loginUser != null) {
            // 登录成功,返回token
            String token = UUID.randomUUID( ).toString( );
            tokenMap.put("token", token);
            tokenMap.put(token, loginUser);
            session.setAttribute("sysUser", loginUser);
            return R.ok(tokenMap);
        } else {
            // 登录失败,返回错误信息
            System.out.println("登录失败");
            return R.fail();
        }
    }
    @MyLog(value = "注册", type = "修改")
    @PostMapping("/user/register")
    public R register(@RequestBody SysUser sysUser) throws Exception {
        //先对前端传过来的密码解密
        String pwd = RsaUtils.decryptByPrivateKey(sysUser.getPassword( ));
        sysUser.setPassword(pwd);
        // 再对前端传过来的password加密
        String pwd2 = PwdEncrypt.hashPassword(sysUser.getPassword( ));
        sysUser.setPassword(pwd2);
        // 添加用户
        Boolean flag = sysUserService.save(sysUser);
        if (flag) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    /**
     * 根据token返回user信息
     * @param token
     * @return
     */
    @MyLog(value = "获得存储用户信息", type = "查询")
    @GetMapping("/user/info")
    public R getUserInfo(String token,HttpSession session){
        // 根据token从redis中获取用户信息
        SysUser user = (SysUser)session.getAttribute("sysUser");
        if (user != null) {
            return R.ok(user);
        } else {
            return R.fail( );
        }
    }

    /**
     * 系统用户退出
     */
    @MyLog(value = "退出", type = "修改")
    @PostMapping("/user/logout")
    public R logout(@RequestBody String token) {
        // 根据token从redis中删除用户信息
        tokenMap.remove(token);
        return R.ok( );
    }
    /**
     * 修改用户信息
     */
    @MyLog(value = "修改用户信息", type = "修改")
    @PostMapping("/user/update")
    public R update(@RequestBody SysUser sysUser) throws Exception {
        //先对前端传过来的密码解密
        String pwd = RsaUtils.decryptByPrivateKey(sysUser.getPassword( ));
        sysUser.setPassword(pwd);
        // 再对前端传过来的password加密
        String pwd2 = PwdEncrypt.hashPassword(sysUser.getPassword( ));
        sysUser.setPassword(pwd2);
        // 修改用户信息
        Boolean flag = sysUserService.update(sysUser);
        return R.ok(flag);
    }
}
